<?php

namespace app\command;

use app\common\utils\ApiReturn;
use app\common\utils\Init;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;

class BaseCommand extends Command
{
    private $description = ''; //任务描述
    private $argumentsData = []; //命令行参数数组
    private $argumentsRequiredCount = 0; //命令行必传参数数量

    public function __construct()
    {
        try {
            parent::__construct();

            //初始化设置
            $this->initSet();
        } catch (\Exception|\Error $e) {
            ApiReturn::retError($e);
        }
    }

    private function initSet()
    {
        Init::setRequestId();
        Init::setRequestStartTime();
        Init::setLogSuffix('command');
    }

    /**
     * 覆盖父类addArgument方法,增加自定义逻辑
     * @param string $name
     * @param int|null $mode
     * @param string $description
     * @param $default
     * @return BaseCommand
     */
    public function addArgument(string $name, int $mode = null, string $description = '', $default = null)
    {
        if ($mode == Argument::REQUIRED) {
            $this->argumentsRequiredCount++;
        }
        $this->argumentsData[] = [
            'name' => $name,
            'mode' => $mode,
            'description' => $description,
        ];
        return parent::addArgument($name, $mode, $description, $default);
    }

    /**
     * 覆盖父类addOption方法,增加自定义逻辑
     * @param string $name
     * @param string|null $shortcut
     * @param int|null $mode
     * @param string $description
     * @param $default
     * @return BaseCommand
     */
    public function addOption(string $name, ?string $shortcut = NULL, ?int $mode = NULL, string $description = '', $default = NULL)
    {
        $this->optionsData[] = [
            'name' => $name,
            'mode' => $mode,
            'description' => $description,
        ];
        return parent::addOption($name, $shortcut, $mode, $description, $default);
    }

    /**
     * 覆盖父类setDescription方法,增加自定义逻辑
     * @param string $description
     * @return BaseCommand
     */
    public function setDescription(string $description)
    {
        $this->description = $description;
        return parent::setDescription($description);
    }

    /**
     * 输出 addArgument 自定义的参数,用于执行的时候在命令行提示
     * @return void
     */
    protected function outputArgumentNotice()
    {
        $argv = $_SERVER["argv"];
        if (count($argv) > 1) {
            $str = "";
            $str .= "---------------------------------------------------- \n";
            $str .= "任务描述: " . $this->description . " \n";
            foreach ($this->argumentsData as $k => $v) {
                $num = $k + 1;
                $mode_text = $v['mode'] == 1 ? '必传参数' : '可选参数';
                $str .= "参数{$num}: {$v['name']} | {$mode_text} | {$v['description']} \n";
            }
            foreach ($this->optionsData as $k => $v) {
                $str .= "option参数: --{$v['name']}=xxx | {$v['description']} \n";
            }
            $str .= "---------------------------------------------------- \n";
            echo $str;
        }
    }

}